// pages/shopxq/shopxq.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabtype: false,   //下面box是否固定
    townid:'1508',
    obj:{},
    typeindex: 0,
    isdown: 1,
    page: 1,
    maxpage: '',
    list: [],
    shopid:'',
    isOverShare: true,
    fenleiisshow:false,
    fenleiid:'',
    huodongtype:'',
    huodongtext:'全部活动',
    huodonglist:[
      {
        text:'全部活动',
        type:''
      },
      {
        text:'传统批发',
        type:0
      },
      {
        text: '满减专区',
        type:4
      },
      {
        text: '满送专区',
        type: 5
      },
      {
        text: '限时抢购',
        type: 6
      },
      {
        text: '大众拼团',
        type: 7
      }
    ]
  },
  choosefenlei(e){
    var _this = this;
    var id = e.currentTarget.dataset.id;
    var text = e.currentTarget.dataset.text;
    wx.showLoading({
      title: '加载中',
    })
    var type;
      type = id
    this.setData({
      huodongtype:type,
      huodongtext:text,
      page:1,
      fenleiisshow: false
    })
    

    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getWsShopGoodsList",
        goodsTypeId: '', //商品分类id
        searchKey: "", //搜索关键字
        sortRule: _this.data.typeindex, //0-推荐 1-销量 2-价格
        sortOrder: _this.data.isdown, //0-升序 1-降序
        nowPage: _this.data.page,
        townId: wx.getStorageSync('townid'),
        shopId: _this.data.shopid,
        wssubType:type
      },
      success: function success(res) {
        console.log(res.data)
        _this.setData({
          maxpage: res.data.totalPage,
          list: res.data.goodsList,
          
        })
        wx.hideLoading()
      }
    });
  },
  fenleibtn(){
    var fenleiisshow = this.data.fenleiisshow;
    this.setData({
      fenleiisshow: !fenleiisshow
    })

  },
  // 跳转客服
  tokefu(){
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    var _this = this;
    var rctoken = this.data.obj.shopRcToken;
    var shopid = this.data.obj.shopId;
    console.log(rctoken)
    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getRectokenInfo",
        flag:1,   //0-普通用户 1-店铺
        // userId: rctoken,   //普通用户ID (flag为0时必传)
        shopId: shopid,    //店铺ID    (flag为1时必传)
      },
      success: function success(res) {
        console.log(res.data)
        var name = res.data.beans[0].nickName;
        var img = res.data.beans[0].photo;
        if (res.data.result == 0) {
          wx.navigateTo({
            url: '../kefu/kefu?friendId=' + rctoken + '&friendName=' + name + '&friendAvatarUrl=' + img,
          })
        } 
      }
    });
  },
  // 轮播图跳链接
  tobannerxq(e){
    var _this = this;
    var type = e.currentTarget.dataset.type;
    var value = e.currentTarget.dataset.value;
    if(value){
      if(type == 4){
        wx.navigateTo({
          url: '../goodsxq/goodsxq?goodsid='+value+'&type=0',
        })
      }else if(type == 6){
        wx.navigateTo({
          url: '../goodsxq/goodsxq?goodsid=' + value + '&type=1',
        })
      } else if (type == 7) {
        wx.navigateTo({
          url: '../goodsxq/goodsxq?goodsid=' + value + '&type=2',
        })
      } else if (type == 8) {
        wx.navigateTo({
          url: '../goodsxq/goodsxq?goodsid=' + value + '&type=3',
        })
      } else if (type == 9) {
        wx.navigateTo({
          url: '../goodsxq/goodsxq?goodsid=' + value + '&type=4',
        })
      }else{
        console.log('no url')
      }
    }
  },
  // 跳转到店铺搜索
  toshopsearch(){
    var shopid = this.data.shopid;
    wx.navigateTo({
      url: '../shopsearch/shopsearch?shopid='+shopid,
    })
  },
  // 跳转商品详情
  togoodsxq(e){
    var _this = this;
    var id = e.currentTarget.dataset.goodsid;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../goodsxq/goodsxq?goodsid='+id+'&type='+type,
    })
  },
  // 切换商品排序
  changeindex(e){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    var i = e.currentTarget.dataset.index;
    var typeindex = this.data.typeindex;
    if (typeindex == i && i != 0) {
      if (_this.data.isdown == 1) {
        _this.setData({
          isdown:0
        })
      } else {
        _this.setData({
          isdown: 1
        })
      }
    } else {
      _this.setData({
        isdown: 1,
        typeindex:i
      })
    }
    this.setData({
      page:1
    })
    this.getshop()
  },
  //获取列表
  getshop() {
    var _this = this;

    wx.request({
      url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getWsShopGoodsList",
        goodsTypeId: _this.data.fenleiid, //商品分类id
        searchKey: "", //搜索关键字
        sortRule: _this.data.typeindex, //0-推荐 1-销量 2-价格
        sortOrder: _this.data.isdown, //0-升序 1-降序
        nowPage: _this.data.page,
        townId: wx.getStorageSync('townid'),
        shopId: _this.data.shopid,
        wssubType: _this.data.huodongtype
      },
      success: function success(res) {
        console.log(res.data)
        
        _this.setData({
          maxpage: res.data.totalPage,
          list: res.data.goodsList
        })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh(); 
        wx.hideLoading()
      }
    });
    
  },
  //打电话
  totel() {
    var tel = this.data.obj.shopPhone;
    wx.makePhoneCall({
      phoneNumber: tel //仅为示例，并非真实的电话号码
    })
  },
  onPageScroll: function (e) {
    var _this = this;
    const query = wx.createSelectorQuery().in(this)
    query.select('#tabbox').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (ress) {
      // console.log(ress[0])
      if (ress[0].top > 82) {
        _this.setData({
          tabtype: false,
          // uptype: true,
        })
      } else {
        _this.setData({
          tabtype: true,
          // uptype: true,
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var shopid;
    if (options.shopid){
      shopid = options.shopid;
    }else{
      shopid = app.globalData.morenshopid
    }
    this.setData({
      shopid:shopid
    })
    wx.showLoading({
      title: '加载中',
    })
    if (!wx.getStorageSync('townid')) {
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          console.log(res)
          wx.request({
            url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
            method: "post",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              cmd: 'getTownId',
              lng: res.longitude,		//地理经度
              lat: res.latitude,		//地理纬度
            },
            success: function success(ress) {
              console.log(ress.data);
              wx.setStorageSync("townid", ress.data.townId);
              var townid = ress.data.townId;
              wx.request({
                url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
                method: "post",
                header: {
                  "content-type": "application/x-www-form-urlencoded"
                },
                data: {
                  cmd: "getWsShopDetail",
                  // userId: app.globalData.userid, //用户ID
                  townId: townid,
                  shopId: shopid,
                },
                success: function success(res) {
                  console.log(res.data.shopDetail)
                  res.data.shopDetail.shopScore = parseInt(res.data.shopDetail.shopScore * 100 /5 ) + '%'
                  wx.setNavigationBarTitle({
                    title: res.data.shopDetail.shopName
                  })
                  _this.setData({
                    obj: res.data.shopDetail
                  })

                }
              });

            }
          });
        }
      })
    }else{
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getWsShopDetail",
          // userId: app.globalData.userid, //用户ID
          townId: wx.getStorageSync('townid'),
          shopId: shopid,
        },
        success: function success(res) {
          console.log(res.data.shopDetail)
          res.data.shopDetail.shopScore = parseInt(res.data.shopDetail.shopScore * 100 / 5) + '%'
          wx.setNavigationBarTitle({
            title: res.data.shopDetail.shopName
          })
          _this.setData({
            obj: res.data.shopDetail
          })

        }
      });
    }
    
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    this.setData({
      page:1
    })
    var timer = setInterval(function () {
      if (wx.getStorageSync('townid')) {
        _this.getshop();
        clearInterval(timer)
      }
    }, 100)
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.page = 1;
    wx.showNavigationBarLoading();
    this.getshop();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    if (_this.data.maxpage > _this.data.page) {
      wx.showLoading({
        title: '加载中',
      })
      var page = _this.data.page;
      page++;
      this.setData({
        page:page
      })
      wx.request({
        url: 'https://sdzxiangmu.com/ZJservice', //自己的服务接口地址
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          cmd: "getWsShopGoodsList",
          goodsTypeId: _this.data.fenleiid, //商品分类id
          searchKey: "", //搜索关键字
          sortRule: _this.data.typeindex, //0-推荐 1-销量 2-价格
          sortOrder: _this.data.isdown, //0-升序 1-降序
          nowPage: page,
          townId: wx.getStorageSync('townid'),
          shopId: _this.data.shopid,
          wssubType: _this.data.huodongtype
        },
        success: function success(res) {
          // console.log(res.data)
          var list = _this.data.list;
          list = list.concat(res.data.goodsList)
          _this.setData({
            list: list
          })
          wx.hideLoading()
        }
      });
    }else{
      wx.showToast({
        title: '没有更多商品啦~',
        icon:'none',
        duration:1500
      })
    }


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // var _this = this;
    // var obj = this.data.obj;
    // return {
    //   path: "/pages/index/index?goodsid=" + obj.shopId + '&type=9',
    //   imageUrl: obj.shopLogo,
    //   title: obj.shopName,
    // };
    return {
      title: wx.getStorageSync('shopName'),
      path: "/pages/shopping/shopxq/shopxq?shopid=" + wx.getStorageSync('shopId'), // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: wx.getStorageSync('shopImg')
    }
  }
})